﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BL;
using Database;
using DB;

namespace UI
{
	public partial class TableForm : Form
	{
		int activeRow = 0, activeCol = 0;
		string oldContent;

		public TableForm()
		{
			InitializeComponent();
			UpdateView();
		}

		private DataGridViewComboBoxColumn GetColumn(int index)
		{
			switch (index)
			{
				case 0: return Monday;
				case 1: return Tuesday;
				case 2: return Wednesday;
				case 3: return Thursday;
				case 4: return Friday;
				case 5: return Saturday;
			}
			return null;
		}

		private void UpdateView()
		{
			dataGridTable.CellValueChanged -= new DataGridViewCellEventHandler(CellValueChange);

			for (int i = 0; i < 6; i++)
			{
				GetColumn(i).Items.Clear();
				GetColumn(i).Items.Add("");
			}
			Subjects subj = Subjects.Instance;
			foreach (SubjectItem si in subj.Items)
			{
				for (int j = 0; j < 6; j++)
					GetColumn(j).Items.Add(si.ToString());
			}

			int k = 0;
			dataGridTable.Rows.Clear();
			Table table = Table.Instance;
			foreach (TableRow row in table.Rows)
			{
				dataGridTable.Rows.Add(row.Cells);
				dataGridTable.Rows[k].HeaderCell.Style.Alignment = 
					DataGridViewContentAlignment.MiddleCenter;
				string label = ((int)(k / 2 + 1)).ToString();
				if (k % 2 == 0)
					dataGridTable.Rows[k].HeaderCell.Value = label;
				k++;
			}

			dataGridTable.CellValueChanged += new DataGridViewCellEventHandler(CellValueChange);
		}

		private void CellValueChange(object sender, DataGridViewCellEventArgs e)
		{
			Table table = Table.Instance;
			string value = "";
			object cell = dataGridTable.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
			if (cell != null)
				value = cell.ToString();
			TableItem ti = table.ChangeCell(e.RowIndex, e.ColumnIndex, value);
			TableItemDB tiMapper = new TableItemDB();
			if (value != oldContent)
			{
				if (value == "")
					tiMapper.DeleteEntity(ti);
				else
				{
					if (oldContent == "")
						tiMapper.Insert(ti);
					else
						tiMapper.UpdateEntity(ti);
				}
			}
		}

		private void FormatCell(object sender, DataGridViewCellFormattingEventArgs e)
		{
			e.CellStyle.ForeColor = (e.RowIndex % 2 == 0) ? Color.Red : Color.Blue;
		}

		private void PostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
		{
			if (e.RowIndex % 2 == 0)
				return;
			int y = e.RowBounds.Bottom - 1;
			int left = dataGridTable.RowHeadersWidth;
			int right = left + dataGridTable.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
			Pen pen = new Pen(Color.Black, 2);
			e.Graphics.DrawLine(pen, 0, y, right, y);
		}

		private void ClickCell(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == -1 || e.RowIndex == -1)
				return;
			activeRow = e.RowIndex;
			activeCol = e.ColumnIndex;
			oldContent = "";
			object cell = dataGridTable.Rows[activeRow].Cells[activeCol].Value;
			if (cell != null)
				oldContent = cell.ToString();
		}

		private void EndResize(object sender, EventArgs e)
		{
			int width = (Size.Width - dataGridTable.RowHeadersWidth - 10) / 6;
			Monday.Width = width;
			Tuesday.Width = width;
			Wednesday.Width = width;
			Thursday.Width = width;
			Friday.Width = width;
			Saturday.Width = width;
			int height = (Size.Height - 25) / 16 - 2;
			for (int i = 0; i < 16; i++)
				dataGridTable.Rows[i].Height = height;
		}
	}
}